﻿using Common;
using NB.MGJ.Common.API;
using NB.MGJ.Common.Helper;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace NB.MGJ.API
{
   public class ApiMGJScanCodeController:ApiBaseController<LogOnUserModel>
    {
     private CodeManageService _codeManageService;
        private FleeGoodsService _flee;
        private ManagerService _enterprise;
        private ProductService _product;
        public ApiMGJScanCodeController()
        {
            _codeManageService = new CodeManageService();
            _flee = new FleeGoodsService();
            _enterprise=new ManagerService();
            _product=new ProductService();
        }


        [Route("nbapi/codemanager/code/search/wxadmin")]
        [HttpGet]
        public IHttpActionResult ScanCode_Two(string code)
        {
            if (string.IsNullOrWhiteSpace(code)) { return ApiFail("该码不存在"); }

            bool _WL_Code = true;

            string _Code = code;

            if (code.Length > 18)
            {
                _WL_Code = false;
                _Code = MiMa.Jiemi(code, AppConst.DigitCodeKey);
            }
            AntiFake_Digitcode _model = _flee.GetDigitcodeModel(code, _WL_Code ? code : _Code, _WL_Code);
            if (_model != null)
            {
               return ApiSuccess(_product.UpdateModelDataForScanCode(_model));
            }
            return ApiSuccess(true);
        }


        [Route("nbapi/codemanager/code/search")]
        [HttpGet]
        public IHttpActionResult ScanCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code)) { return ApiFail("该码不存在"); }

            bool _WL_Code = true;

            string _Code = code;

            if (code.Length > 18)
            {
                _WL_Code = false;
                _Code = MiMa.Jiemi(code, AppConst.DigitCodeKey);
            }
            AntiFake_Digitcode _model = _flee.GetDigitcodeModel(code, _WL_Code ? code : _Code, _WL_Code);
            if (_model != null)
            {
                if (_model.EnterpriseId != 0)
                {
                    _model.EnterpriseName = _enterprise.OrganizationalShowDataId(_model.EnterpriseId).EnterpriseName ?? "末知";
                }
                else
                {
                    _model.EnterpriseName = "末知";
                }
                if (_model.ProductId != 0)
                {
                    _model.ProductName = _product.GetProModel(_model.ProductId).ProductName ?? "未绑定";
                }
                else
                {
                    _model.ProductName = "末绑定";
                }

                _model.Batch = _model.ProductBatchId != 0 ? _product.GetProductBatchName(_model.ProductBatchId) ?? "未知" : "末绑定";
               
                if (_model.QueryCount == 0)
                {
                    _model.QueryCount = 1;
                    _model.QueryTime = DateTime.Now;
                }
                else
                {
                    _model.QueryCount++;
                }
                string _tb_name = string.Format("nbcodemain.AntiFake_Digitcode{0}", _WL_Code ? code.Substring(0, 8) : _Code.Substring(0, 8));
                _codeManageService.UpdateDigitcode(_model, _tb_name);
                _model.Message = "";
                return ApiSuccess(_model);
            }
            return ApiFail("该码不存在");
        }

        //查询溯源
        public bool SearchOrigins(string code)
        {
            string real_fw_code = MiMa.Jiemi(code, AppConst.DigitCodeKey);
            AntiFake_Digitcode model = _flee.GetTable(real_fw_code);
            if (model != null)
            {
                return true;
            }
            else return false;

        }

        #region 忘记密码
        [ApiVisitFilter("FindPassword")]
        [Route("nbapi/my/forgetpwd")]
        [HttpPost]
        public IHttpActionResult ChangePwd([FromBody]dynamic obj)
        {
            try
            {
                string loginName = obj.loginName;
                string newpwd = obj.pwd;
                Sys_User user = _enterprise.GetUserInfoByName(loginName);
                user.Pwd = EDS3.DESEnCode(newpwd);
                return ApiSuccess(_enterprise.EditUserInfoService(user));
            }
            catch (Exception e)
            {
                return ApiFail(e.Message);
            }
        }
        #endregion
    }
}
